﻿<template>
  <!-- 报检单 -->
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('WmsOrder:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="单据编号">
                <a-input v-model="queryParam.no" allow-clear placeholder="请输入单据编号" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="送货通知单号">
                <a-input v-model="queryParam.sourceBillNo" allow-clear placeholder="请输入送货通知单号" />
              </a-form-item> </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="供应商编码">
                  <a-input v-model="queryParam.vendorcode" allow-clear placeholder="请输入供应商编码" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="供应商名称">
                  <a-input v-model="queryParam.vendorname" allow-clear placeholder="请输入供应商名称" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="公司名称">
                  <a-input v-model="queryParam.companyname" allow-clear placeholder="请输入公司名称" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="仓管员">
                  <a-input v-model="queryParam.warehouseKeepername" allow-clear placeholder="请输入仓管员" />
                </a-form-item> </a-col><a-col :md="8" :sm="24">
                <a-form-item label="贸易方式">
                  <a-select :allowClear="true" style="width: 100%" v-model="queryParam.tradeMode" placeholder="请选择贸易方式">
                    <a-select-option v-for="(item, index) in tradeModeData" :key="index" :value="item.code">{{
                      item.name
                    }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="仓储中心">
                  <a-input v-model="queryParam.warehouseCentername" allow-clear placeholder="请输入仓储中心" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="仓间">
                  <a-input v-model="queryParam.storeRoomname" allow-clear placeholder="请输入仓间" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="报关单号">
                  <a-input v-model="queryParam.declarationCode" allow-clear placeholder="请输入报关单号" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="专业">
                  <a-input v-model="queryParam.majorname" allow-clear placeholder="请输入专业" />
                </a-form-item>
              </a-col>
            </template>

            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table ref="table" :columns="columns" :data="loadData" :alert="true" :rowKey="record => record.id"
        :scroll="{ x: true }" :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }">
        <template class="table-operator" slot="operator" v-if="hasPerm('WmsOrder:add')">
          <!-- <a-button type="info" v-if="hasPerm('sysExcelTemplate:importExcel')" icon="upload" @click="$refs.excelForm.index()">
            导入
          </a-button>
          <a-button type="info" v-if="hasPerm('wmsOrder:exportExcel')" icon="download" @click="wmsOrderToExcel()">
            导出
          </a-button>
          <a-button type="primary" v-if="hasPerm('WmsOrder:add')" icon="plus" @click="$refs.addForm.add()">新增单据表</a-button> -->
        </template>
        <span slot="tradeModescopedSlots" slot-scope="text">
          {{ 'trade_mode' | dictType(text) }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('WmsOrder:edit')" @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('WmsOrder:edit') & hasPerm('WmsOrder:delete')" />
          <!-- <a-popconfirm v-if="hasPerm('WmsOrder:delete')" placement="topRight" title="确认删除？" @confirm="() => WmsOrderDelete(record)">
            <a>删除</a>
          </a-popconfirm> -->
        </span>

        <template slot="linkSlot" slot-scope="text, record">
          <a @click="onOpenInfo(record)">{{ text }}</a>
        </template>

        <span slot="action" slot-scope="text, record">
          <!-- <a :class="[(record.orderStatus===1||record.orderStatus===4)?'':'disabled']" style="margin-right:10px;" @click="onEdit(record)">编辑</a> -->
          <a v-if="hasPerm('WmsReceiptOrder:edit')" :class="[record.orderStatus == 1 ? 'disabled' : '']"
            @click="onDistribute(record)">报检</a>

          <!-- <a :class="[record.orderStatus === 1 || record.orderStatus === 4 ? '' : 'disabled']" style="margin-right:10px;"
            @click="$refs.editForm.edit(record)">编辑</a>
          <a-dropdown>
            <a class="ant-dropdown-link" @click="e => e.preventDefault()">更多...</a>
            <a-menu slot="overlay">
              <a-menu-item><a :class="[record.orderStatus === 1 || record.orderStatus === 4 ? '' : 'disabled']"
                  style="margin-right:10px;" @click="onDistribute(record)">下发</a></a-menu-item>
              <a-menu-item><a :class="[record.orderStatus === 1 || record.orderStatus === 4 ? '' : 'disabled']"
                  @click="WmsOrderDelete(record)">删除</a></a-menu-item>
            </a-menu>
          </a-dropdown> -->
        </span>

        <span slot="containerStatusscopedSlots" slot-scope="text">
          <a-tag :color="text === 2 ? '#daa520' : text === 1 ? '#cd5c5c' : text === 3 ? '#add8e6' : '#696969'">{{
            'order_statusenum' | dictType(text)
          }}</a-tag>
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
      <detail-drawer ref="tableDetailDrawer" :visible.sync="infoVisible" :row="infoRow" @postBjData="postBjData" />
    </a-card>
  </div>
</template>
<script>
import { STable } from '@/components'
import { WmsOrderPage, WmsOrderDelete, WmsOrderBaojian } from '@/api/modular/main/WmsOrderInspectionManage'
import addForm from './addForm.vue'
import editForm from './editForm.vue'
import DetailDrawer from './DetailDrawer.vue'

export default {
  components: {
    STable,
    addForm,
    editForm,
    DetailDrawer
  },
  data() {
    return {
      advanced: false, // 高级搜索 展开/关闭
      queryParam: {},
      columns: [
        {
          title: '单据编号',
          align: 'center',
          dataIndex: 'no',
          scopedSlots: { customRender: 'linkSlot' },
          width: '100px'
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'orderStatus',
          scopedSlots: { customRender: 'containerStatusscopedSlots' },
          width: '100px'
        },

        {
          title: '供应商编码',
          align: 'center',
          dataIndex: 'vendorcode',
          width: '100px'
        },
        {
          title: '供应商名称',
          align: 'center',
          dataIndex: 'vendorname',
          width: '100px'
        },


        {
          title: '创建日期',
          align: 'center',
          dataIndex: 'billdate',
          width: '100px'
        },
        {
          title: '公司名称',
          align: 'center',
          dataIndex: 'companyname',
          width: '100px'
        },
        {
          title: '报关单号',
          align: 'center',
          dataIndex: 'declarationCode',
          width: '100px'
        },

        {
          title: '专业',
          align: 'center',
          dataIndex: 'majorname',
          width: '100px'
        },

        {
          title: '送货通知单号',
          align: 'center',
          dataIndex: 'sourceBillNo',
          width: '100px'
        },


        {
          title: '仓储中心',
          align: 'center',
          dataIndex: 'warehouseCentername',
          width: '100px'
        },

        {
          title: '仓管员',
          align: 'center',
          dataIndex: 'warehouseKeepername',
          width: '100px'
        },

        {
          title: '备注',
          align: 'center',
          dataIndex: 'remarks',
          width: '100px'
        },

        // {
        //   title: '送货通知单号',
        //   align: 'center',
        //   dataIndex: 'sourceBillNo',
        //   width: '100px'
        // },

        // {
        //   title: '公司名称',
        //   align: 'center',
        //   dataIndex: 'companyname',
        //   width: '100px'
        // },
        // {
        //   title: '仓管员',
        //   align: 'center',
        //   dataIndex: 'warehouseKeepername',
        //   width: '100px'
        // },
        // {
        //   title: '贸易方式',
        //   align: 'center',
        //   dataIndex: 'tradeMode',
        //   scopedSlots: { customRender: 'tradeModescopedSlots' },
        //   width: '100px'
        // },
        // {
        //   title: '仓储中心',
        //   align: 'center',
        //   dataIndex: 'warehouseCentername',
        //   width: '100px'
        // },
        // {
        //   title: '仓间',
        //   align: 'center',
        //   dataIndex: 'storeRoomname',
        //   width: '100px'
        // },
        // {
        //   title: '报关单号',
        //   align: 'center',
        //   dataIndex: 'declarationCode',
        //   width: '100px'
        // },
        // {
        //   title: '专业',
        //   align: 'center',
        //   dataIndex: 'majorname',
        //   width: '100px'
        // },


      ],
      tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return WmsOrderPage(Object.assign(parameter, this.queryParam)).then(res => {
          return res.data
        })
      },
      tradeModeData: [],
      selectedRowKeys: [],
      selectedRows: [],
      infoRow: {},
      infoVisible: false,
      containerStatusData: [],
      postBjDataArr: []
    }
  },
  created() {
    if (this.hasPerm('WmsOrder:edit') || this.hasPerm('WmsOrder:delete')) {
      this.columns.push({
        title: '操作',
        width: '100px',
        dataIndex: 'action',
        fixed: 'right',
        scopedSlots: { customRender: 'action' }
      })
    }
    const tradeModeOption = this.$options
    this.tradeModeData = tradeModeOption.filters['dictData']('trade_mode')

    const containerStatusOption = this.$options
    this.containerStatusData = containerStatusOption.filters['dictData']('order_statusenum')
  },
  methods: {
    /**
     * 查询参数组装
     */
    switchingDate() {
      const obj = JSON.parse(JSON.stringify(this.queryParam))
      return obj
    },
    WmsOrderDelete(record) {
      WmsOrderDelete(record).then(res => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('删除失败') // + res.message
        }
      })
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    onOpenInfo(row) {
      this.infoVisible = true
      this.infoRow = row
      this.$refs.tableDetailDrawer.getList(row)
    },

    onDistribute(obj) {
      // if (obj.orderStatus !== 1 && obj.orderStatus !== 4) return false
      if (obj.orderStatus == 1) return false
      if(this.postBjDataArr.length<=0 || obj.id != this.postBjDataArr[0].orderId){
        this.$message.error('请选中此单据下的物料!')
        return
      }
      this.$confirm({
        title: '系统提示',
        content: '您将要进行报检任务的操作，确认要继续嘛？',
        okText: '确认',
        cancelText: '取消',
        onOk: () => {
          let param = {
            ...obj,
            dtls: this.postBjDataArr
          }
          WmsOrderBaojian(param).then(res => {
            if (res.success) {
              this.$message.success('操作成功')
              this.$refs.table.refresh()
            } else {
              this.$message.error('操作失败')
            }
          })
        }
      })
    },
    // 选中的物料
    postBjData(data) {
      this.postBjDataArr = data
    }
  }
}
</script>
<style lang="less">
.table-operator {
  margin-bottom: 18px;
}

button {
  margin-right: 8px;
}
</style>
